package com.dragon.www.smarthome.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.entity.Device;
import com.dragon.www.smarthome.entity.DeviceInfo;
import com.dragon.www.smarthome.entity.SingleReturnInfo;
import com.dragon.www.smarthome.net.NetHelper;
import com.dragon.www.smarthome.net.NetUtils;
import com.dragon.www.smarthome.view.BaseItemView;

/**
 * Created by liqp on 2016/3/16.
 */
public class EntranceGuardAdapter extends AppArrayAdapter<Device, EntranceGuardAdapter.EntranceGuardViewHolder>  {

    private Toast toast;

    public EntranceGuardAdapter(Context context){
        super(context, R.layout.entrance_guard_view_item, EntranceGuardViewHolder.class);
    }
    public View bindHolder(View convertView, ViewGroup parent, int position) {
        final Device entranceguard = this.devices.get(position);
        EntranceGuardViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new EntranceGuardViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.entrance_guard_view_item, parent, false);
            viewHolder.tv_entranceguard_name = (TextView) convertView.findViewById(R.id.tv_entrance_guard_desc);
            viewHolder.tv_entranceguard_status = (BaseItemView) convertView.findViewById(R.id.baseItemView_entrance_guard);
            viewHolder.entrance_guard_open = (ImageView) convertView.findViewById(R.id.entrance_guard_open);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (EntranceGuardViewHolder) convertView.getTag();
        }

        viewHolder.tv_entranceguard_name.setText(entranceguard.getDeviceName());
        viewHolder.tv_entranceguard_status.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (com.dragon.www.smarthome.util.Utils.isFastClick(1000)) {
                        if (toast == null) {
                            toast = Toast.makeText(context,  "请不要过快操作", Toast.LENGTH_SHORT);
                        } else {
                            toast.cancel();//关闭吐司显示
                            toast = Toast.makeText(context,  "请不要过快操作", Toast.LENGTH_SHORT);
                        }
                        toast.show();
                        return false;
                    }
                    if (deviceInfo != null ){
                        if ( Integer.parseInt(entranceguard.getequipNo().split("-")[0]) == box1){
                            setAction(entranceguard, 1,box1);

                        }else if ( Integer.parseInt(entranceguard.getequipNo().split("-")[0]) == box2){
                            setAction(entranceguard, 1,box2);
                        }
                    }

                }
                return false;
            }
        });

        if (deviceInfo != null ){
            if ( Integer.parseInt(entranceguard.getequipNo().split("-")[0]) == box1) {
                if (deviceInfo[0].getRelayValues()[7] == 0) {

                    viewHolder.tv_entranceguard_status.turnOff();

                } else if (deviceInfo[0].getRelayValues()[7] == 1) {
                    viewHolder.tv_entranceguard_status.turnOn();

                }
            }else if( Integer.parseInt(entranceguard.getequipNo().split("-")[0]) == box2) {
                if (deviceInfo[1].getRelayValues()[7] == 0) {
                    viewHolder.tv_entranceguard_status.turnOff();
                } else if (deviceInfo[1].getRelayValues()[7] == 1) {
                    viewHolder.tv_entranceguard_status.turnOn();
                }
            }
        }
        return convertView;
    }

    public void setAction (final Device entranceguard, int action,int boxid){
        NetHelper.sendAction(context, boxid, 1, Integer.parseInt(entranceguard.getequipNo().split("-")[1]), "power", action, "relay", new NetUtils.NetCallBack<DeviceInfo>() {

            @Override
            public void success(DeviceInfo rspData) {
                if (rspData.getError() == 0) {

                } else {
                    String description = rspData.getDescription();
                    Log.e("error", description);
                }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }, SingleReturnInfo.class);

    }

    public class EntranceGuardViewHolder extends AppHolder {
        TextView tv_entranceguard_name;
        BaseItemView tv_entranceguard_status;
        ImageView entrance_guard_open;
    }
}

